package anyVehicle;

import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.TileEntity;
import net.minecraft.src.World;

public class TileEntityScaffold extends TileEntity {
	protected int widthBlocks;
	protected int heightBlocks;
	protected int lengthBlocks;

	public TileEntityScaffold(World world, int x, int y, int z) {
		worldObj = world;
		this.xCoord = x;
		this.yCoord = y;
		this.zCoord = z;
		setSizeBlocks(VehicleConfiguration.MAX_WIDTH + 2, VehicleConfiguration.MAX_HEIGHT + 2, VehicleConfiguration.MAX_LENGTH + 2);
	}

	public TileEntityScaffold() {

	}

	@Override
	public void writeToNBT(NBTTagCompound nbttagcompound) {
		super.writeToNBT(nbttagcompound);
		nbttagcompound.setInteger("lengthBlocks", lengthBlocks);
		nbttagcompound.setInteger("heightBlocks", heightBlocks);
		nbttagcompound.setInteger("widthBlocks", widthBlocks);
	}

	@Override
	public void readFromNBT(NBTTagCompound nbttagcompound) {
		super.readFromNBT(nbttagcompound);
		setSizeBlocks(nbttagcompound.getInteger("lengthBlocks"), nbttagcompound.getInteger("heightBlocks"), nbttagcompound.getInteger("widthBlocks"));

	}

	public Vehicle createCustomBoat() {
		int x = (int) xCoord;
		int y = (int) yCoord;
		int z = (int) zCoord;
		Vehicle entity;
		if (VehicleConfiguration.duplicateShip) {
			entity = new Vehicle(worldObj, x + 1, y + 1, z + getLengthBlocks() + 2, x, y, z, 0.1D); // TODO
																									// Find
																									// the
																									// right
																									// parameters
		} else {
			// TODO Find the right parameters
			entity = new Vehicle(worldObj, x + getLengthBlocks() / 2, y + getHeightBlocks() / 2, z + getWidthBlocks() / 2, x, y, z, 0.1D); 

			// Determines the water level
			int waterLevel = VehicleUtil.findWaterLevel(worldObj, x + 1, y + 1, z + 1, entity.getLengthBlocks(), entity.getHeightBlocks(),
					entity.getWidthBlocks());
			
			// Erases the space the new entity will occupy
			VehicleUtil.createCube(worldObj, x + 1, waterLevel + 1, z + 1, entity.getLengthBlocks(), y + entity.getHeightBlocks() - waterLevel,
					entity.getWidthBlocks(), 0);
			System.out.println( (x + 1) + ", "+ (waterLevel + 1)+ ", "+  (z + 1)+ ", "+  entity.getLengthBlocks() + ", "+  (y + entity.getHeightBlocks() - waterLevel) + ", "+ 
					entity.getWidthBlocks());
			// Fills with water the space below waterLevel
			VehicleUtil.createCube(worldObj, x + 1, y + 1, z + 1, entity.getLengthBlocks(), waterLevel-y,	entity.getWidthBlocks(), 9);
			System.out.println( (x + 1) + ", "+ (y + 1)+ ", "+  (z + 1)+ ", "+  entity.getLengthBlocks() + ", "+  (waterLevel-y)  + ", "+ 
					entity.getWidthBlocks());
		}
		worldObj.entityJoinedWorld(entity);
		return entity;
	}

	public void setWidthBlocks(int widthBlocks) {
		this.widthBlocks = widthBlocks;
		if (this.widthBlocks < VehicleConfiguration.MIN_WIDTH) {
			this.widthBlocks = VehicleConfiguration.MIN_WIDTH;
		}

		if (this.widthBlocks > VehicleConfiguration.MAX_WIDTH) {
			this.widthBlocks = VehicleConfiguration.MAX_WIDTH;
		}
	}

	public void setHeightBlocks(int heightBlocks) {
		this.heightBlocks = heightBlocks;
		if (this.heightBlocks < VehicleConfiguration.MIN_HEIGHT) {
			this.heightBlocks = VehicleConfiguration.MIN_HEIGHT;
		}
		if (this.heightBlocks > VehicleConfiguration.MAX_HEIGHT) {
			this.heightBlocks = VehicleConfiguration.MAX_HEIGHT;
		}
	}

	public void setLengthBlocks(int lengthBlocks) {
		this.lengthBlocks = lengthBlocks;
		if (this.lengthBlocks < VehicleConfiguration.MIN_LENGTH) {
			this.lengthBlocks = VehicleConfiguration.MIN_LENGTH;
		}
		if (this.lengthBlocks > VehicleConfiguration.MAX_LENGTH) {
			this.lengthBlocks = VehicleConfiguration.MAX_LENGTH;
		}

	}

	public void setSizeBlocks(int l, int h, int w) {
		lengthBlocks = l;
		heightBlocks = h;
		widthBlocks = w;
	}

	public int getWidthBlocks() {
		return widthBlocks;
	}

	public int getHeightBlocks() {
		return heightBlocks;
	}

	public int getLengthBlocks() {
		return lengthBlocks;
	}

}
